using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog.Web;
using System;

namespace BJoin.MicroSVC.Sample.Webapi
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
            CreateHostBuilder(args).Build().Run();
            logger.Info("the microsvc.sample program init success");
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((context,cfgBuilder)=>
                {
                    var config = cfgBuilder.Build();
                    //get config values from config
                    var changedReload = config["ChangedReload"];
                    if (changedReload.Equals("true", StringComparison.OrdinalIgnoreCase))
                    {
                        var hostEnv = context.HostingEnvironment.EnvironmentName;
                        cfgBuilder
                            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                            .AddJsonFile($"appsettings.{hostEnv}.json", optional: true, reloadOnChange: true);
                    }
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    //webBuilder.UseUrls("http://*:5185;http://*:5186");
                })
                .ConfigureLogging((hostContext, logBuilder) =>
                {
                    logBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
                })
                .UseNLog();
    }
}
